package com.cjl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjl.anno.OperLog;
import com.cjl.auth.service.SysPostService;
import com.cjl.commons.result.Result;
import com.cjl.model.system.SysPost;
import com.cjl.model.system.SysUser;
import com.cjl.vo.system.SysPostQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author cjl
 * @since 2023-03-18
 */
@Api(tags = "岗位管理接口")
@RestController
@RequestMapping("/admin/system/sysPost")
@CrossOrigin
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;

    //条件分页查询
    @ApiOperation("岗位条件分页查询")
    @GetMapping("{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    @OperLog(businessType = "SELECT", title = "岗位管理")
    public Result index(@PathVariable("page") Long page,
                        @PathVariable("limit") Long limit,
                        SysPostQueryVo sysPostQueryVo){
        Page<SysPost> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        String postName = sysPostQueryVo.getName();
        String postCode = sysPostQueryVo.getPostCode();

        if(!StringUtils.isEmpty(postName)){
            wrapper.like(SysPost::getName, postName);
        }
        if(!StringUtils.isEmpty(postCode)){
            wrapper.eq(SysPost::getPostCode, postCode);
        }
        Page<SysPost> pageModel = sysPostService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取岗位")
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    @OperLog(businessType = "SELECT", title = "岗位管理")
    public Result get(@PathVariable Long id) {
        SysPost sysPost = sysPostService.getById(id);
        return Result.ok(sysPost);
    }

    @ApiOperation(value = "保存岗位")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('bnt.sysPost.add')")
    @OperLog(businessType = "SELECT", title = "岗位管理")
    public Result save(@RequestBody SysPost sysPost) {
        sysPostService.save(sysPost);
        return Result.ok();
    }

    @ApiOperation(value = "更新岗位")
    @PostMapping("update")
    @PreAuthorize("hasAuthority('bnt.sysPost.update')")
    @OperLog(businessType = "UPDATE", title = "岗位管理")
    public Result update(@RequestBody SysPost sysPost) {
        sysPostService.updateById(sysPost);
        return Result.ok();
    }

    @ApiOperation(value = "删除岗位")
    @DeleteMapping("remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysPost.remove')")
    @OperLog(businessType = "DELETE", title = "岗位管理")
    public Result remove(@PathVariable Long id){
        sysPostService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @OperLog(businessType = "UPDATE", title = "岗位管理")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysPostService.updateStatus(id, status);
        return Result.ok();
    }

    @ApiOperation(value = "获取岗位")
    @GetMapping("/toAssign/{userId}")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @OperLog(businessType = "SELECT", title = "岗位管理")
    public Result toAssign(@PathVariable("userId") Long userId){
        Map<String, Object> map = sysPostService.findPostByUserId(userId);
        System.out.println(map);
        return Result.ok(map);
    }
}

